<script src="../../common/js/quill-config.js"></script>
<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-select v-model="filters.state" placeholder="状态">
					<el-option label="已处理" value="1"></el-option>
					<el-option label="未处理" value="0"></el-option>
				</el-select>
				<el-form-item>
					<el-button type="primary" v-on:click="loadListData">查询</el-button>
				</el-form-item>

			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="searchMasterMsgs" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="title" label="标题" >
			</el-table-column>
			<el-table-column prop="name" label="宠物名字" >
			</el-table-column>
			<el-table-column prop="age" label="年龄" >
			</el-table-column>
			<el-table-column prop="gender" label="性别" >
				<template scope="scope">
					<span style="color: #11b95c" v-if="scope.row.gender===0">母</span>
					<span style="color: red" v-else-if="scope.row.gender===1">公</span>
					<span style="color: plum" v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column prop="price" label="价格" >
			</el-table-column>
			<el-table-column prop="address" label="地址" >
			</el-table-column>
			<el-table-column prop="state" label="状态" >
				<template scope="scope">
					<span style="color: #11b95c" v-if="scope.row.state===1">已处理</span>
					<span style="color: red" v-else-if="scope.row.state===0">待处理</span>
					<span style="color: plum" v-else>未知</span>
				</template>
			</el-table-column>

			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">处理</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<el-dialog :title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="服务名字" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="原价">
					<el-input v-model="addForm.costprice"></el-input>
				</el-form-item>
				<el-form-item label="折扣价">
					<el-input v-model="addForm.saleprice"></el-input>
				</el-form-item>
				<el-form-item label="产品图片">
					<el-upload
							class="upload-demo"
							action="/api/fastDfs/upload"
							:on-remove="handleRemove"
							:on-success="uploadSuccess"
							:file-list="fileList"
							list-type="picture">
						<el-button size="small" type="primary">点击上传</el-button>
						<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
					</el-upload>
				</el-form-item>
				<el-form-item label="产品详情">
					<!--<el-input v-model="addForm.detail.intro"></el-input>-->
					<quill-editor  v-model="addForm.detail.intro" :options="quillConfig"></quill-editor>
				</el-form-item>
				<el-form-item label="预约须知">
					<!--<el-input v-model="addForm.detail.orderNotice"></el-input>-->
					<quill-editor  v-model="addForm.detail.orderNotice" :options="quillConfig"></quill-editor>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'

    import { quillEditor } from 'vue-quill-editor'
	//引入 配置文件js
	import quillConfig from '../../common/js/quill-config'

	export default {
        components:{
            quillEditor
		},
		data() {
			return {
                quillConfig:quillConfig,
                title:'新增',
                fileList:[],
				filters: {
					name: '',
					state:null
				},
				searchMasterMsgs: [],
				total: 0,
                currentPage: 1,
				listLoading: false,
				sels: [],//列表选中列
                pageSize:5,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},


				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入用户名', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
                    id:null,
					name:'',
					costprice:0,
					saleprice:0,
					resources:0,
					detail:{
                        intro:'',
						orderNotice:''
					}
				}

			}
		},
		methods: {
		    //点击删除图片 执行的逻辑
            handleRemove(file, fileList) {
                let res = '';
                //拼接上传结果
                for(var i=0;i<fileList.length;i++ ){
                    if(i!=fileList.length-1){
                        res+=fileList[i].response.resultObj+",";
                    }else {
                        res+=fileList[i].response.resultObj;
                    }

                }
                this.addForm.resources=res;
            },
			//上传成功的处理方案
            uploadSuccess(response, file, fileList){
                //把响应的地址赋值给 shop对象里面的logo
               //this.addForm.resources=response.resultObj;
                //多次上传的结果进行拼接 AA.jpg,bb.jpg,pp.jpg
				//console.debug(fileList)
				let res = '';
				//拼接上传结果
				for(var i=0;i<fileList.length;i++ ){
				    if(i!=fileList.length-1){
				        res+=fileList[i].response.resultObj+",";
					}else {
                        res+=fileList[i].response.resultObj;
					}

				}
                this.addForm.resources=res;
            },
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.loadListData();
			},
			//获取用户列表
			loadListData() {
                let param = {
                    currentPage:this.currentPage,
                    pageSize:5,
                    keyword:this.filters.name,
					state:this.filters.state
                }
			    this.$http.post("/searchMasterMsg",param).then(res=>{
                    //res  请求返回的数据
                    this.searchMasterMsgs=res.data.rows;
                    this.total=res.data.total;
                })
            },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认立即处理消息?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/searchMasterMsg/"+row.id).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						let {success,message} =res.data;
						if (success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
						}else {
                            this.$message({
                                message: '网络异常!!',
                                type: 'error'
                            });
						}
						this.loadListData();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
                this.fileList=[];//清空图片显示列表
                this.title="编辑";
				this.addFormVisible = true;
				this.addForm = Object.assign({}, row);
				//回显图片 本质--给 fileList[{name:xxx ,url:sss}] 赋值
				if(row.resources){
				    //分隔 resources
                    let arrs =row.resources.split(",");
                    for(var i=0;i<arrs.length;i++){
                        this.fileList.push({
							name:'产品.jpg',
							url:'http://121.37.194.36'+arrs[i]
						})
					}

				}


			},
			//显示新增界面
			handleAdd: function () {
                this.fileList=[];//清空图片列表
                this.title="新增";
				this.addFormVisible = true;
				//this.addForm = Object.assign({}, {});
				this.addForm= {
                    id: null,
                    name: '',
                    costprice: 0,
                    saleprice: 0,
                    resources: '',
                    detail: {
                        id:null,
                        intro: '',
                        orderNotice: ''
                    }
                }
                this.addForm.state=0;//对状态特殊处理 默认就是下架
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);

							this.$http.put("/product",para).then((res) => {
								this.addLoading = false;
								let {success,message}=res.data;
								if(success){
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                }else{
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
								}

								this.addFormVisible = false;
								this.loadListData();
								this.fileList=[];
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
            onSale(){
				// 哪些数据要上架  id
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认上架选中记录吗？', '提示', {
                    type: 'info'
                }).then(()=>{
                    this.listLoading = true;
                    //NProgress.start();
                    //let para = { ids: ids };
                    this.$http.patch("/product/onSale",ids).then(res=>{
                        this.listLoading = false;
                        this.$message({
                            message: '上架成功',
                            type: 'success'
                        });

						this.loadListData()
					})

				})

			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.loadListData();
		}
	}

</script>

<style scoped>

</style>